package mt.dummy;

import static org.junit.Assert.*;

import java.io.OutputStream;

import mt.Order;
import mt.comm.ClientSideMessage;
import mt.comm.ServerSideMessage;

import org.junit.Test;

public class allTests {

	@Test
	public void testConnect() {
		ClientCommDummy clientCommD = new ClientCommDummy();
		clientCommD.connect("127.0.0.1", "joao");
	}

	@Test
	public void testIsConnected() {
		ClientCommDummy clientCommD = new ClientCommDummy();
		assertFalse(clientCommD.isConnected());
	}

	@Test
	public void testDisconnect() {
	}

	@Test
	public void testHasNextMessageClientCommDummy() {
		ClientCommDummy clientCommD = new ClientCommDummy();
		assertFalse(clientCommD.hasNextMessage());
	}

	@Test
	public void testGetNextMessageClientCommDummy() {
		ClientCommDummy clientCommD = new ClientCommDummy();
		assertNull(clientCommD.getNextMessage());

	}

	@Test
	public void testSendOrderClientCommDummy() {
		ClientCommDummy clientCommD = new ClientCommDummy();
		clientCommD.sendOrder(null);
	}

	@Test
	public void testClientMessageTypeString() {
		ClientMessage clientMessage = new ClientMessage(
				ClientSideMessage.Type.ERROR, "error");
		assertEquals(clientMessage, clientMessage);
		assertNotNull(clientMessage);
	}

	@Test
	public void testClientMessageTypeOrder() {
		Order order = null;
		ClientMessage clientMessage = new ClientMessage(
				ClientSideMessage.Type.ERROR, order);
		assertEquals(clientMessage, clientMessage);
		assertNotNull(clientMessage);
	}

	@Test
	public void testGetType() {
		ClientMessage clientMessage = new ClientMessage(
				ClientSideMessage.Type.ERROR, "error");
		assertNotNull(clientMessage);
		assertEquals(ClientSideMessage.Type.ERROR, clientMessage.getType());
	}

	@Test
	public void testGetOrder() {
		Order order = null;
		ClientMessage clientMessage = new ClientMessage(
				ClientSideMessage.Type.ERROR, order);
		assertNotNull(clientMessage);
		// assertNull(clientMessage.getOrder());
		assertEquals(null, clientMessage.getOrder());
	}

	@Test
	public void testGetError() {
		ClientMessage clientMessage = new ClientMessage(
				ClientSideMessage.Type.ERROR, "This is the error string!");
		assertEquals("This is the error string!", clientMessage.getError());
	}
	
	@Test
	public void testRemove() {
		HashMaps<String, OutputStream> maps= new HashMaps<String, OutputStream>();	
		assertTrue(maps.isEmpty());
		maps.addEntry("Test1", null);
		maps.addEntry("Test2", null);
		maps.addEntry("Test3", null);
		assertFalse(maps.isEmpty());
		maps.remove("Test1");
		assertFalse(maps.contains("Test1"));
	}

	@Test
	public void testContains() {
		HashMaps<String, OutputStream> maps= new HashMaps<String, OutputStream>();
		assertTrue(maps.isEmpty());
		maps.addEntry("Test1", null);
		maps.addEntry("Test2", null);
		maps.addEntry("Test3", null);
		assertFalse(maps.isEmpty());
		assertTrue(maps.contains("Test1"));
		assertTrue(maps.contains("Test2"));
		assertTrue(maps.contains("Test3"));
	}

	@Test
	public void testClear() {
		HashMaps<String, OutputStream> maps= new HashMaps<String, OutputStream>();	
		assertTrue(maps.isEmpty());
		maps.addEntry("Test1", null);
		maps.addEntry("Test2", null);
		maps.addEntry("Test3", null);
		assertFalse(maps.isEmpty());
		maps.clear();
		assertTrue(maps.isEmpty());
	}

	@Test
	public void testAddEntry() {
		HashMaps<String, OutputStream> maps= new HashMaps<String, OutputStream>();
		assertTrue(maps.isEmpty());
		maps.addEntry("Test1", null);
		maps.addEntry("Test2", null);
		maps.addEntry("Test3", null);
		assertFalse(maps.isEmpty());
	}

	@Test
	public void testGet() {
		HashMaps<String, OutputStream> maps= new HashMaps<String, OutputStream>();
		assertTrue(maps.isEmpty());
		maps.addEntry("Test1", null);
		maps.addEntry("Test2", null);
		maps.addEntry("Test3", null);
		assertFalse(maps.isEmpty());
		assertEquals(null, maps.get("Test2"));
	}
	
	@Test
	public void testStart() {
		ServerCommDummy serverCommD= new ServerCommDummy();
				serverCommD.start();
		
	}

	@Test
	public void testHasNextMessageServerCommDummy() {
		ServerCommDummy serverCommD= new ServerCommDummy();
		assertFalse(serverCommD.hasNextMessage());
		ServerMessage msg = new ServerMessage(ServerSideMessage.Type.CONNECTED, "name", null) ;
		serverCommD.q.add(msg);
		assertTrue(serverCommD.hasNextMessage());
	}

	@Test
	public void testGetNextMessageServerCommDummy() {
		ServerCommDummy serverCommD= new ServerCommDummy();
		assertNull(serverCommD.getNextMessage());
		ServerMessage msg = new ServerMessage(ServerSideMessage.Type.CONNECTED, "name", null) ;
		serverCommD.q.add(msg);
		assertTrue(serverCommD.hasNextMessage());
		assertNotNull(serverCommD.getNextMessage());
	}

	@Test
	public void testSendErrorServerCommDummy() {
		ServerCommDummy serverCommD= new ServerCommDummy();
		serverCommD.sendError("jose", "erro");
		ServerMessage msg = new ServerMessage(ServerSideMessage.Type.CONNECTED, "ana", null) ;
		serverCommD.q.add(msg);
		serverCommD.maps.addEntry(msg.getSenderNickname(), null);
		serverCommD.sendError("ana", "erro");

	}

	@Test
	public void testClientIsConnected() {
		ServerCommDummy serverCommD= new ServerCommDummy();
		assertFalse(serverCommD.clientIsConnected("jose"));
		ServerMessage msg = new ServerMessage(ServerSideMessage.Type.CONNECTED, "jose", null) ;
		serverCommD.q.add(msg);
		serverCommD.maps.addEntry(msg.getSenderNickname(), null);
		assertTrue(serverCommD.clientIsConnected("jose"));	}

	@Test
	public void testDisconnectClient() {
		ServerCommDummy serverCommD= new ServerCommDummy();
		ServerMessage msg = new ServerMessage(ServerSideMessage.Type.CONNECTED, "jose", null) ;
		serverCommD.q.add(msg);
		serverCommD.maps.addEntry(msg.getSenderNickname(), null);
		assertTrue(serverCommD.clientIsConnected("jose"));	
		serverCommD.disconnectClient("jose");
		assertFalse(serverCommD.clientIsConnected("jose"));
	}

	@Test
	public void testSendOrderServerCommDummy() {
		ServerCommDummy serverCommD= new ServerCommDummy();
		assertFalse(serverCommD.clientIsConnected("joana"));
		serverCommD.sendOrder("joana", null);
		ServerMessage msg = new ServerMessage(ServerSideMessage.Type.CONNECTED, "joana", null) ;
		serverCommD.q.add(msg);
		serverCommD.maps.addEntry(msg.getSenderNickname(), null);
		assertTrue(serverCommD.clientIsConnected("joana"));
		serverCommD.sendOrder("joana", null);
	}
	
	@Test
	public void testServerMessage() {
		String senderNickname = "Software Eng";
		String stock = "EDP";
		int numberOfUnits = 5000;
		double pricePerUnit = 10;
		Order order = Order.createBuyOrder(senderNickname, stock,
				numberOfUnits, pricePerUnit);
		ServerMessage serverMessage = new ServerMessage(
				ServerSideMessage.Type.CONNECTED, senderNickname, order);
		assertNotNull(serverMessage);
	}

	@Test
	public void testGetTypeServerMessage() {
		String senderNickname = "Software Eng";
		String stock = "EDP";
		int numberOfUnits = 5000;
		double pricePerUnit = 10;
		Order order = Order.createBuyOrder(senderNickname, stock,
				numberOfUnits, pricePerUnit);
		ServerMessage serverMessage = new ServerMessage(
				ServerSideMessage.Type.CONNECTED, senderNickname, order);
		assertNotNull(serverMessage.getType());
		assertEquals(ServerSideMessage.Type.CONNECTED, serverMessage.getType());
	}

	@Test
	public void testGetSenderNickname() {
		String senderNickname = "Software Eng";
		String stock = "EDP";
		int numberOfUnits = 5000;
		double pricePerUnit = 10;
		Order order = Order.createBuyOrder(senderNickname, stock,
				numberOfUnits, pricePerUnit);
		ServerMessage serverMessage = new ServerMessage(
				ServerSideMessage.Type.CONNECTED, senderNickname, order);
		assertNotNull(serverMessage.getSenderNickname());
		assertEquals(senderNickname, serverMessage.getSenderNickname());
	}

	@Test
	public void testGetOrderServerMessage() {
		String senderNickname = "Software Eng";
		String stock = "EDP";
		int numberOfUnits = 5000;
		double pricePerUnit = 10;
		Order order = Order.createBuyOrder(senderNickname, stock,
				numberOfUnits, pricePerUnit);
		ServerMessage serverMessage = new ServerMessage(
				ServerSideMessage.Type.CONNECTED, senderNickname, order);
		assertEquals(order, serverMessage.getOrder());
	}
}
